﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace prjYan.Forum.Controllers
{
    public class UploadController : Controller
    {
        //
        // GET: /Upload/

        public string UploadImage(HttpPostedFileBase fileData)
        {
            fileData = Request.Files[0];
            string strDate = DateTime.Now.Year.ToString() + DateTime.Now.ToString("MM");
            string fName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(fileData.FileName);
            string ftpUploadUrl = string.Empty;
            try
            {
                Byte[] buffer = new Byte[1];
                buffer = YanForum.Engine.Services.Common.SaveImage(new Bitmap(fileData.InputStream), 100L);

                string strFtpAddress = YanForum.Engine.Services.BaseService.FtpServerPath;
                string strFolderUser = YanForum.Engine.Services.Common.GetFolderByDate(strFtpAddress);
                YanForum.Engine.Services.Common.FtpCreateFolder(strFolderUser, YanForum.Engine.Services.Common.GetFtpUser(), YanForum.Engine.Services.Common.GetFtpPass());
                ftpUploadUrl = strFolderUser + fName;

                YanForum.Engine.Services.Common.UpFileServer(ftpUploadUrl, buffer);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return strDate + "/" + fName;
        }

        public string SaveThumb(string imagePath, int? cropPointX, int? cropPointY, int? imageCropWidth, int? imageCropHeight)
        {
            string strDate = imagePath.Substring(0, 6);
            string strName = imagePath.Substring(7);

            string strFTP = YanForum.Engine.Services.BaseService.FtpServerPath + strDate + "/1x1_" + strName;
            string strImage = YanForum.Engine.Services.BaseService.ImgServerPath + strDate + "/" + strName;

            int intW = int.Parse(imageCropWidth.ToString());
            int intH = int.Parse(imageCropHeight.ToString());
            int intX = int.Parse(cropPointX.ToString());
            int intY = int.Parse(cropPointY.ToString());

            YanForum.Engine.Services.Common.CropUploadImage(strImage, strFTP, intW, intH, intX, intY);
            return null;
        }
    }
}
